package com.lanou.demo.backgrounddemo.controller;

import com.lanou.demo.backgrounddemo.service.ItemService;
import com.lanou.entity.entity.ItemDescEntity;
import com.lanou.entity.entity.ItemEntity;
import com.lanou.entity.entity.ItemParamItemEntity;
import com.lanou.entity.entity.TreeEntity;
import com.lanou.entity.utils.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 常宏盛
 * Date: 2019/7/11 10:58
 **/
@RestController
public class ItemController {

    @Resource
    private ItemService itemService;

    /**
     * 商品分页查询
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/item/list")
    public AjaxResult list(int page, int rows) {
        int count = this.itemService.count();
        List<ItemEntity> itemList = this.itemService.list(page, rows);
        return AjaxResult.builder().total(count).rows(itemList).build();
    }

    /**
     * 新增商品和商品描述
     *
     * @param item
     * @return
     */
    @RequestMapping("/item/save")
    public AjaxResult save(ItemEntity item, String itemParams) {
        item.setId((long) (Math.random() * 90000 + 10000));
        int itemSaveResult = this.itemService.itemSave(item);
        int itemDescSaveResult = this.itemService.itemDescSave(item.getDesc(), item.getId());
        int itemParamSaveResult = this.itemService.itemParamSave(item.getId(), itemParams);
        if (itemSaveResult == 0 || itemDescSaveResult == 0 || itemParamSaveResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

    /**
     * 删除商品
     *
     * @param ids
     * @return
     */
    @RequestMapping("/rest/item/delete")
    public AjaxResult delete(String[] ids) {
        int deleteResult = this.itemService.delete(ids);
        if (deleteResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

    /**
     * 下架商品
     *
     * @param ids
     * @return
     */
    @RequestMapping("/rest/item/instock")
    public AjaxResult instock(String[] ids) {
        int instockResult = this.itemService.instock(ids);
        if (instockResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

    /**
     * 上架商品
     *
     * @param ids
     * @return
     */
    @RequestMapping("/rest/item/reshelf")
    public AjaxResult reshelf(String[] ids) {
        int reshelfResult = this.itemService.reshelf(ids);
        if (reshelfResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

    /**
     * 加载商品描述
     *
     * @param id
     * @return
     */
    @RequestMapping("/rest/item/query/item/desc/{id}")
    public AjaxResult itemDesc(@PathVariable Long id) {
        ItemDescEntity itemDesc = this.itemService.itemDesc(id);
        if (itemDesc == null) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).data(itemDesc).build();
    }

    /**
     * 加载商品规格
     *
     * @param id
     * @return
     */
    @RequestMapping("/rest/item/param/item/query/{id}")
    public AjaxResult itemParam(@PathVariable Long id) {
        ItemParamItemEntity paramData = this.itemService.itemParam(id);
        if (paramData == null) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).data(paramData).id(paramData.getId()).build();
    }

    /**
     * 选择类目
     *
     * @param parentId
     * @return
     */
    @RequestMapping("/item/cat/list")
    public List<TreeEntity> itemCatList(@RequestParam(name = "id", defaultValue = "0") Long parentId) {
        return this.itemService.itemCatList(parentId);
    }

    /**
     * 编辑商品
     *
     * @param item
     * @return
     */
    @RequestMapping("/rest/item/update")
    public AjaxResult update(ItemEntity item, String itemParams, Long itemParamId) {
        int updateResult = this.itemService.update(item);
        int updateItemParamDataResult = this.itemService.updateItemParamData(itemParams, itemParamId);
        if (updateResult == 0 || updateItemParamDataResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

}
